package com.mc.decisiontree;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class DecisionTree {
	
	//计算信息熵
	public static double calcShannonEnt(DataType data){
		HashMap<Integer,Integer> count = new HashMap<Integer,Integer>();
		for(int i=0;i<data.length();i++){
			if(data.isMarked(i))continue;
			int label = data.getLable(i);
			if(count.containsKey(label))
				count.put(label, count.get(label)+1);
			else
				count.put(label, 1);
		}
		double entropy = 0;
		Iterator<Entry<Integer,Integer>> iter = count.entrySet().iterator();
		while(iter.hasNext()){
			Entry<Integer,Integer> entry = iter.next();
			int value = entry.getValue();
			double freq = value/data.unMarkedLength();
			entropy-=Math.log(freq)/Math.log(2)*freq;
		}
		return entropy;
	}
}
